154B - Colliders - CodeForces Solution


math number theory *1600

Please click on ads to support us..

Python Code:

from http.client import SWITCHING_PROTOCOLS
from sys import stdin, stdout

def main():
    n = int(1e5)
    lp = [0 for _ in range(n+1)]
    pr= []

    for i in range(2,n+1):
        if lp[i] == 0:
            lp[i] = i
            pr.append(i)

        j = 0
        while j<len(pr) and i*pr[j]<=n and pr[j]<=lp[i]:
            lp[i*pr[j]] = pr[j]
            j+=1

    visited = [0 for _ in range(n+1)]

    n, m = map(int,stdin.readline().split())
    for _ in range(m):
                a,b = stdin.readline().strip().split()
        b=int(b)
        if visited[b]==b:
            if a == '+':
                stdout.write("Already on\n")
                continue
            else:
                stdout.write("Success\n")
                visited[b] = 0
                if lp [b] !=b:
                    B = b
                    facs = []
                    while B!=1:
                        f = lp[B]
                        facs.append(f)
                        while B%f == 0:
                            B//=f
                    for f in facs:
                        visited[f] = 0
                continue
        if lp[b]==b:
            if a == '-':
                stdout.write('Already off\n')
                continue
                        if visited[b]:
                stdout.write('Conflict with '+str(visited[b])+'\n')
            else:
                visited[b]=b
                stdout.write('Success\n')
        else:
            if a == '-':
                stdout.write('Already off\n')
                continue
            B = b
            facs = []
            while B!=1:
                f = lp[B]
                facs.append(f)
                while B%f == 0:
                    B//=f

            flag = True
            for f in facs:
                if visited[f]:
                    stdout.write('Conflict with '+str(visited[f])+'\n')
                    flag = False
                    break
            if not flag:
                continue
            visited[b] = b
            for f in facs:
                visited[f] = b
            stdout.write('Success\n')

if __name__=='__main__':
    main()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n";
//-----------------------------------------------------------------------------------------
   #define db_bhai        ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
   #define test           int T; cin>>T; for(int t=1;t<=T;t++)
   #define ll             long long
   #define lli            long long int
   #define ld             long double
   #define yes            cout<<"YES"<<endl
   #define no             cout<<"NO"<<endl
   #define f(i,x,n)       for(int i = x; i < n; i++)
   #define fo(i,x,n)      for(int i=x;i>=n;i--)
   #define pb             push_back
   #define pp             pop_back()
   #define all(v)         (v).begin(), (v).end()
   #define array_le_le    lli a[n]; f(i,0,n){cin>>a[i];}
   #define vector_le_le   vector<lli>v; f(i,0,n){int x; cin>>x; v.pb(x);}
//-----------------------------------------------------------------------------------------
   const long long        INF=1e18;
   const int32_t          MM=998244353;
   const int              N=1e7+5;
   const int32_t          M=1e9+7;
//-----------------------------------------------------------------------------------------
ll __lcm(ll a,ll b){return (a*b)/__gcd(a,b);}
const ll MOD = 1e9 + 7;
const double eps=1e-6;

int32_t main(){
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      cout.tie(NULL);

      ll n,m;
      cin>>n>>m;
      map<ll,ll>mp;
      map<ll,ll>mpp;
      for(int i=0;i<m;i++)
      {
         char c;
         ll tmp;
         cin>>c>>tmp;
         if(c=='+')
         {
            if(mp[tmp]==1)
            {
               cout<<"Already on";
            }
            else
            {
               bool flag=0;
               ll ans;
               
               for(int j=2;j<=sqrt(tmp);j++)
               {
                  if(tmp%j==0)
                  {
                     if(mpp[j]>0)
                     {
                        flag=1;
                        ans=mpp[j];
                     }
                     else if(mpp[tmp/j]>0)
                     {
                        flag=1;
                        ans=mpp[tmp/j];
                     }
                  }
               }
               if(mpp[tmp]>0)
               {
                  ans=mpp[tmp];
                  flag=1;
               }
               



               if(flag)
               {
                  cout<<"Conflict with "<<ans;
               }
               else
               {
                  cout<<"Success";
                  mp[tmp]=1;
                  for(int j=2;j<=sqrt(tmp);j++)
                  {
                     if(tmp%j==0)
                     {
                        mpp[j]=tmp;
                        mpp[tmp/j]=tmp;
                     }
                  }
                  mpp[tmp]=tmp;
               }
            }
         }
         else
         {
               if(mp[tmp])
               {
                  cout<<"Success";
                  for(int j=2;j<=sqrt(tmp);j++)
                  {
                     if(tmp%j==0)
                     {
                        mpp[j]=0;
                        mpp[tmp/j]=0;
                     }
                  }
                  mpp[tmp]=0;
                  mp[tmp]--;
               }
               else
               {
                  cout<<"Already off";
               }
         }
         cout<<endl;
      }




      return 0;

}


Comments

Submit
0 Comments
More Questions

1506D - Epic Transformation
1354G - Find a Gift
1426F - Number of Subsequences
1146B - Hate "A"
1718C - Tonya and Burenka-179
834A - The Useless Toy
1407D - Discrete Centrifugal Jumps
1095B - Array Stabilization
291B - Command Line Arguments
1174B - Ehab Is an Odd Person
624B - Making a String
1064C - Oh Those Palindromes
1471A - Strange Partition
1746A - Maxmina
1746B - Rebellion
66C - Petya and File System
1746C - Permutation Operations
1199B - Water Lily
570B - Simple Game
599C - Day at the Beach
862A - Mahmoud and Ehab and the MEX
1525A - Potion-making
1744D - Divisibility by 2n
1744C - Traffic Light
1744A - Number Replacement
1744B - Even-Odd Increments
637B - Chat Order
546C - Soldier and Cards
18D - Seller Bob
842B - Gleb And Pizza